package com.vcc.service.impl;


import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.core.util.DateUtil;
import com.vcc.core.util.RegexUtil;
import com.vcc.data.entity.KycCertInfo;
import com.vcc.data.entity.Regulation;
import com.vcc.data.mapper.RegulationMapper;
import com.vcc.service.sa.BossUserUtil;
import com.vcc.service.service.RegulationService;
import org.springframework.stereotype.Service;

import static com.vcc.data.entity.table.BossUserTableDef.BOSS_USER;
import static com.vcc.data.entity.table.RegulationTableDef.REGULATION;
import static com.vcc.data.entity.table.UserTableDef.USER;

@Service
public class RegulationServiceImpl extends ServiceImpl<RegulationMapper, Regulation> implements RegulationService {
    @Override
    public Page<Regulation> paging(Page<Regulation> pageable, String type, Long targetUserId, Integer useFlag, String startDate, String endDate, Long dealerId) {
        QueryChain<Regulation> queryChain = queryChain();
        queryChain.leftJoin(USER).on(USER.ID.eq(REGULATION.USER_ID));
        queryChain.leftJoin(BOSS_USER.as("dealer")).on(USER.BE_LONG_TO_DEALER.eq(BOSS_USER.as("dealer").ID));
        queryChain.leftJoin(BOSS_USER.as("opera")).on(REGULATION.OPERATION_USER_ID.eq(BOSS_USER.as("opera").ID));
        queryChain.select("(select card_number from vcc_user_card where vcc_user_card.id = vcc_regulation.card_id) as cardNumber");
        queryChain.select(REGULATION.ALL_COLUMNS);
        queryChain.select(USER.EMAIL);
        queryChain.select(BOSS_USER.as("dealer").EMAIL.as("dealerAccount"));
        queryChain.select(BOSS_USER.as("opera").EMAIL.as("operaAccount"));
        Long bossUserId = BossUserUtil.getIdNotNull();
        if (!BossUserUtil.hasAllData()) {
            queryChain.and(
                    BOSS_USER.as("dealer").ID.eq(bossUserId)
                            .or(BOSS_USER.as("dealer").PARENT_ID.eq(bossUserId))
            );
        } else {
            if (RegexUtil.checkObjectIsNotNull(dealerId)) {
                queryChain.and(
                        BOSS_USER.as("dealer").ID.eq(dealerId)
                                .or(BOSS_USER.as("dealer").PARENT_ID.eq(dealerId))
                );
            }
        }
        if (RegexUtil.checkStringIsNotNull(type)) {
            queryChain.eq(Regulation::getType, type);
        }
        if (RegexUtil.checkObjectIsNotNull(targetUserId)) {
            queryChain.eq(Regulation::getUserId, targetUserId);
        }
        if (RegexUtil.checkObjectIsNotNull(useFlag)) {
            queryChain.eq(Regulation::getUseFlag, useFlag);
        }
        if (RegexUtil.checkStringIsNotNull(startDate)) {
            queryChain.ge(Regulation::getRequestDate, DateUtil.getDayBeginDate(startDate, "yyyy-MM-dd"));
        }
        if (RegexUtil.checkStringIsNotNull(endDate)) {
            queryChain.le(Regulation::getRequestDate, DateUtil.getDayEndDate(endDate, "yyyy-MM-dd"));
        }
        queryChain.orderBy(Regulation::getRequestDate, false);
        return queryChain.page(pageable);
    }
}